<?php
class smsClient
{
	private $smsWsdl;
    private $username;
    private $password;    
    private $serviceName;
	    
    public function __construct()
    {
    	$this->smsWsdl = sfConfig::get('sms_wsdl');
    	$this->username = sfConfig::get('sms_user_sent');
    	$this->password = sfConfig::get('sms_pass_sent');
    	$this->serviceName = sfConfig::get('sms_service_name');
    }
    
    //method to log all message output
    private function sentLogging($status, $serviceNumber, $receiver, $msgType, $content, $charge, $description, $smsId)
    {
    	//sfContext::getInstance()->getLogger()->err("SMS logging function called");

   	    $smsOutput = new SmsOutput();
		$smsOutput->setServiceNumber($serviceNumber);
		$smsOutput->setSmsId($smsId);
		$smsOutput->setReceivePhone($receiver);
		$smsOutput->setSendDatetime("now");
		$smsOutput->setSmsType($msgType);
		$smsOutput->setContent($content);
		$smsOutput->setDescription($description);
		$smsOutput->setCharge($charge);
		$smsOutput->setServiceName($this->serviceName);
		$smsOutput->setStatus($status);
		$smsOutput->setNew(true);
		$smsOutput->save();
		
		if ($smsId != null)
		{
			//sfContext::getInstance()->getLogger()->err("SMS logging 1");
			$inpId = $smsOutput->getId();
			//search in sms_input to find the same sms_id
			$cr = new Criteria();
			$cr->add(SmsInputPeer::SMS_ID, $smsId);
			$sms = SmsInputPeer::doSelectOne($cr);
			if ($sms != null)
			{
				$sms->setOutputId($inpId);
				$sms->setNew(false);
				$sms->save();
			}
		}
    }
    
	public function sentMT($serviceNumber, $receiver, $msgType, $content, $charge, $description, $smsId, $senderPhone)
	{
		//sfLoader::loadHelpers('MyString');
        //$content      = strKhongdau($content);
        //$description  = strKhongdau($description);
        
        /*function shutdown()
	  	{
	  		$this->sentLogging(1, $serviceNumber, $receiver, $msgType, $content, $charge, $description, $smsId);
	  	}
		
	  	register_shutdown_function('shutdown');
	  	set_time_limit(15);*/
		
		try
		{
			$client = new SoapClient($this->smsWsdl);
			
			$params = array( 'username' => $this->username, 
		                     'password' => $this->password, 
		                     'service_number' => $serviceNumber,
		                     'sender' => $senderPhone,
							 'receiver' => $receiver,
		                     'content' => $content,
		                     'charge' => $charge,
		                     'msg_type' => $msgType,
							 'subject' => $description,
		                     'sms_id' => $smsId,
							 'smsc' => '9282');
						
            $result = $client->__soapCall('sendMT', array('parameters' => $params));                        
			$this->sentLogging(0, $serviceNumber, $receiver, $msgType, $content, $charge, $description, $smsId);
			return $result;
		}
		catch (Exception $e)
		{
			$result = 'fail';
		}
		
        return $result;		                          
	}
}

?>